﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;


public partial class Property_Info_Statistics : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CensusBtn1.Text = "Social";
        CensusBtn2.Text = "Economic";
        CensusBtn3.Text = "Housing";
        CensusBtn4.Text = "Demographic";

        CensusTableTitle1.Text = "<h2>US Census Data - Social</h2>";
        CensusTableTitle2.Text = "<h2>US Census Data - Economic</h2>";
        CensusTableTitle3.Text = "<h2>US Census Data - Housing</h2>";
        CensusTableTitle4.Text = "<h2>US Census Data - Demographic</h2>";
    }
    protected void StatInit(object sender, EventArgs e)
    {
        CensusTable1.Text = getCensusData(1);
        CensusTable2.Text = getCensusData(2);
        CensusTable3.Text = getCensusData(3);
        CensusTable4.Text = getCensusData(4);

        CensusTableTitle1.Text = "<h2>" + City.Value + " Census Data - Social</h2>";
        CensusTableTitle2.Text = "<h2>" + City.Value + " Census Data - Economic</h2>";
        CensusTableTitle3.Text = "<h2>" + City.Value + " Census Data - Housing</h2>";
        CensusTableTitle4.Text = "<h2>" + City.Value + " Census Data - Demographic</h2>";

        CensusBtn1.Enabled = true;
        CensusBtn2.Enabled = true;
        CensusBtn3.Enabled = true;
        CensusBtn4.Enabled = true;
        
    }
    
    protected string getCensusData(int no) 
    {
        string table_string = "";
        try
        {

            WebClient client = new WebClient();
            
            //The initial value is just for an example - we will need to get the address and city from the database and store it in these strings.
            string mid = "&_county=&_cityTown=";
            string address = Master.Address;
            string city = City.Value;
            
            if (no == 1)
            {
                string start = "http://factfinder.census.gov/servlet/ACSSAFFFacts?_event=Search&geo_id=01000US&_geoContext=01000US&_street=";
                string end = "&_state=04000US39&_zip=&_lang=en&_sse=on&ActiveGeoDiv=search_by_address&_useEV=&pctxt=fph&pgsl=010&_submenuId=factsheet_0&ds_name=ACS_2007_3YR_SAFF&_ci_nbr=null&qr_name=null&reg=null%3Anull&_keyword=&_industry=";

                //Get the HTML source of the first page to parse
                string source = string.Concat(start, address, mid, city, end);
                string s = client.DownloadString(source);

                //Get the unique string to identify the city to get census data from
                int first = s.IndexOf("showADP(");
                string x = s.Remove(0, first + 8);
                string[] Split = x.Split(new Char[] { ',' });
                mid = Convert.ToString(Split[1]);
                mid = mid.Remove(0, 1);
                mid = mid.Remove(mid.Length - 1, 1);

                //Get the source of the set of Social census data
                start = "http://factfinder.census.gov/servlet/ADPTable?_bm=y&-geo_id=";
                end = "&-qr_name=ACS_2007_3YR_G00_DP3YR2&-ds_name=ACS_2007_3YR_G00_&-_lang=en&-redoLog=false&-_sse=on";
                source = string.Concat(start, mid, end);
                string social = client.DownloadString(source);

                //Get the table of Social census data
                first = social.IndexOf("<TR><TD class=");
                social = social.Remove(0, first + 629);
                first = social.IndexOf("<!-- Print");
                social = social.Remove(first, 3618);
                start = "<br>Tables and supporting text retrieved from the American FactFinder on the U.S. Census Bureau Website: <a href='";
                string citation = string.Concat(start, source, "'>", source, "<a>", "<br>");
                social = string.Concat(social, citation);
                table_string = social;           
            }
            else if (no == 2)
            {
                //Get the source of the set of Economic census data
                string start = "http://factfinder.census.gov/servlet/ADPTable?_bm=y&-geo_id=";
                string end = "&-qr_name=ACS_2007_3YR_G00_DP3YR3&-ds_name=ACS_2007_3YR_G00_&-_lang=en&-redoLog=false&-_sse=on";
                string source = string.Concat(start, mid, end);
                string economic = client.DownloadString(source);

                //Get the table of Economic census data
                int first = economic.IndexOf("<TR><TD class=");
                economic = economic.Remove(0, first + 629);
                first = economic.IndexOf("<!-- Print");
                economic = economic.Remove(first, 3618);
                start = "<br>Tables and supporting text retrieved from the American FactFinder on the U.S. Census Bureau Website: <a href='";
                string citation = string.Concat(start, source, "'>", source, "<a>", "<br>");
                economic = string.Concat(economic, citation);
                table_string = economic;    
            }
            else if (no == 3)
            {
                //Get the source of the set of Housing census data
                string start = "http://factfinder.census.gov/servlet/ADPTable?_bm=y&-geo_id=";
                string end = "&-qr_name=ACS_2007_3YR_G00_DP3YR4&-ds_name=ACS_2007_3YR_G00_&-_lang=en&-redoLog=false&-_sse=on";
                string source = string.Concat(start, mid, end);
                string housing = client.DownloadString(source);

                //Get the table of Housing census data
                int first = housing.IndexOf("<TR><TD class=");
                housing = housing.Remove(0, first + 629);
                first = housing.IndexOf("<!-- Print");
                housing = housing.Remove(first, 3618);
                start = "<br>Tables and supporting text retrieved from the American FactFinder on the U.S. Census Bureau Website: <a href='";
                string citation = string.Concat(start, source, "'>", source, "<a>", "<br>");
                housing = string.Concat(housing, citation);
                table_string = housing; 
            }
            else if (no == 4)
            {
                //Get the source of the set of Demographic census data
                string start = "http://factfinder.census.gov/servlet/ADPTable?_bm=y&-geo_id=";
                string end = "&-qr_name=ACS_2007_3YR_G00_DP3YR5&-ds_name=ACS_2007_3YR_G00_&-_lang=en&-redoLog=false&-_sse=on";
                string source = string.Concat(start, mid, end);
                string demographic = client.DownloadString(source);

                //Get the table of Demographic census data
                int first = demographic.IndexOf("<TR><TD class=");
                demographic = demographic.Remove(0, first + 629);
                first = demographic.IndexOf("<!-- Print");
                demographic = demographic.Remove(first, 3618);
                start = "<br>Tables and supporting text retrieved from the American FactFinder on the U.S. Census Bureau Website: <a href='";
                string citation = string.Concat(start, source, "'>", source, "<a>", "<br>");
                demographic = string.Concat(demographic, citation);
                table_string = demographic;
            }
            
        }
        catch(Exception e) {

            table_string = e.Message;
        
        }
        table_string = table_string.Replace("<TR onmouseout=\"unmark_row(this)\" onmouseover=\"mark_row(this)\">", "<TR>");
        return table_string;
    }
}

